<?php
class NavModel extends CommonModel {
	public $nav_path;
	public function __construct(){
		parent::__construct();
		$this->nav_path = C("nav_cache_path");
	}
	function queryList($child_str = 'items' , $where = ''){
		$result = $this->getNavList($where);
		$result = genTree9($result,'id','pid',$child_str);
		
		return $result ; 
		
	}
	//获取导航列表的数据
	
	public function getNavList($where = '' ){
		$nav = C("DB_PREFIX")."admin_nav";
		$getwhere = " 1 = 1 ";
		if($where == '' ){
			$getwhere .= " AND status = '1' " ;
		}
		$list = M("admin_nav")->where($getwhere)->order("disorder asc ")->select();
		//echo M("admin_nav")->getLastSql();
		if($list){
			foreach($list as $k=>$v){
				$result[$v['id']]  = $v ;
			}
		}
		return $result ;
	}
	//把数格式化为二位数组进行遍历
	public function getTreeData($tree  ){
		static $data ;
		foreach($tree as $t){
			$data[] = array(
					'id'=>$t['id'] ,
					'name'=>$t['name'] ,
					'status'=>$t['status'] ,
					'parentid'=>$t['parentid'] ,
					'path'=>$t['path'] ,
					'addtime'=>$t['addtime'] ,
					'url'=>$t['url'] ,
					'disorder'=>$t['disorder'] ,
					'index'=>count(explode("-" , $t['path']))
	
			)  ;
			if(isset($t['items'])){
	
				$this->getTreeData($t['items']  );
			}
		}
		return $data ;
	}
	//添加导航
	public function addNav($params = array() ){
		$exists = $this->getNavByUrl($params['url']);
		if($exists){
			return array('status'=>0, 'message'=>'导航已经存在');
		}
		if($params['pid'] == 0 ){
			$params['path'] = 0 ;
		}else{
			$pidInfo = M("Admin_nav")->where("id={$params['pid']}")->find();
			$params['path'] = $pidInfo['path']."-".$params['pid'];
		}
		$params['addtime'] = date("Y-m-d H:i:s" , time());
		$id =  M("Admin_nav")->add($params);
		$sql = M("Admin_nav")->getLastSql();
		if($id){
			write_action_log($sql, getModuleAction(),$this->username,get_client_ip(),1,"添加导航成功");
			$this->make_nav();
			return array('status'=>1 , 'message'=>'添加导航成功');
		}else{
			write_action_log($sql, getModuleAction(),$this->username,get_client_ip(),0,"添加导航失败");
			return array('status'=>0 , 'message'=>'网络繁忙请稍后！！');
		}
	}
	//查询导航是否存在
	public function getNavByUrl($url){
		$info =  M("Admin_nav")->where("url = '{$url}' ")->find();
		if($info){
			return true ;
		}
		return false ;
	}
	//根据id查询导航信息
	public function getNavInfo($id){
		return M("Admin_nav")->where("id={$id}")->find();
	}
	//修改导航
	public function editNav($params = array() ,$id ){
		$info = M("Admin_nav")->where("id = '{$id}'")->find();
		if(!$info){
			return array('status'=>0 , 'message'=>'暂无查询到导航的信息');
		}
		$num  =  M("Admin_nav")->where("id={$id}")->save($params);
		$sql = M("Admin_nav")->getLastSql();
		if($num >= 1 ){
			$path = $info['pid']."-".$id;
			M("Admin_nav")->where("path like '{$path}%' ")->save(array('status'=>$params['status']) );
			write_action_log($sql, getModuleAction(),$this->username,get_client_ip(),1,"修改导航成功");
			$this->make_nav();
			return array('status'=>1 , 'message'=>'修改导航成功');
		} 
		write_action_log($sql, getModuleAction(),$this->username,get_client_ip(),0,"修改导航失败");
		return array('status'=>0 , 'message'=>'网络繁忙请稍后！！');
	}

	//修改排序
	public function do_disorder($params = array() ,$id){
		$info = M("Admin_nav")->where("id = '{$id}' ")->find();
		if(!$info){
			return array('status'=>0 , 'message'=>'暂无查询到导航的信息');
		}
		$num  =  M("Admin_nav")->where("id={$id}")->save($params);
		$sql = M("Admin_nav")->getLastSql();
		if($num >= 1 ){
			write_action_log($sql, getModuleAction(),$this->username,get_client_ip(),1,"修改导航排序成功");
			return array('status'=>1 , 'message'=>'修改导航排序成功');
		} 
		write_action_log($sql, getModuleAction(),$this->username,get_client_ip(),0,"修改导航排序失败");
		return array('status'=>0 , 'message'=>'网络繁忙请稍后！！');
	}
	//删除导航数据
	public function delNav($id){
		$data = M("Admin_nav")->where("id={$id}")->find();
		if(!$data){
			return false ;
		}
		$num = M("Admin_nav")->where("id={$id}")->delete(); 
		$path = $data['path']."-".$data['id'] ;
		if($num){
			$num = M("Admin_nav")->where("path like '{$path}%'")->delete();
			$sql_child = M("Admin_nav")->getLastSql();
			write_action_log($sql_child, getModuleAction(),$this->username,get_client_ip(),1,"删除导航{$data['name']}和下面的子导航成功成功");
			return true ;
		}else{
			$sql_child = M("Admin_nav")->getLastSql();
			write_action_log($sql_child, getModuleAction(),$this->username,get_client_ip(),0,"删除导航{$data['name']}和下面的子导航成功失败");
			return false ;
		}
	
	}
	//查询导航数据 ， 返回
	public function get_nav(){
		$nav_data = array();
		if(!file_exists($this->nav_path."/nav.inc.php")){
			$this->make_nav() ;
			include_once $this->nav_path."/nav.inc.php" ;
			$nav_data = $nav_config ;
		}else{
			include_once $this->nav_path."/nav.inc.php" ;
			$nav_data = $nav_config ;
		}
		return $nav_data ;
	
	}
	public function make_nav(){
		$list_data = M("Admin_nav")->where("status = '1' ")->select();
		if(!is_writable($this->nav_path)){
			exit("目录".$this->nav_path."不可写,或者不存在");
		}
		$configfile = $this->nav_path."/nav.inc.php";
		$time = date("Y-m-d H:i" , time());
		$fp = fopen($configfile,'w');
		flock($fp,3);
		fwrite($fp,"<"."?php\r\n");
		fwrite($fp,"/*导航基本信息配置*/\r\n");
		fwrite($fp,"/*author wangjian*/\r\n");
		fwrite($fp,"/*time {$time}*/\r\n");
	
		$data = array() ;
		if($list_data){
			foreach($list_data as $j_key=>$j_val){
				if(substr($j_val['url'],-1) == '/'){
					$j_val['url']= substr($j_val['url'],0,-1) ;
				}
				if($j_val['url'] != '' ){
					$data[] = $j_val['url'] ;
				}
					
			}
		}
		
		$string = "\$nav_config = " ;
		$string.= var_export($data , true ) ;
	
		fwrite($fp,"$string;\r\n");
	}	
}
